<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="产品名称">
              <a-input placeholder="请输入项目名称" v-model="queryParam.name"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button @click="() => {this.queryPage()}" type="primary">查询</a-button>
            <a-button @click="() =>{ queryParam = {}}" style="margin-left: 8px">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button @click="(e) => this.goToUpload()" style="margin-bottom: 8px">
        导入
      </a-button>
    </div>
    <s-table
      :columns="columns"
      :data="loadData"
      :rowKey="record => record.id"
      bordered
      ref="table"
    >
    </s-table>
    <product-upload
      @ok="queryPage"
      ref="ProductUpload">
    </product-upload>
  </a-card>

</template>

<script>
import { TableMixin } from '@/mixins/TableMixin'
import { fetchList } from '@api/boms/product'
import ProductUpload from './ProductUpload'

export default {
  components: {
    ProductUpload
  },
  mixins: [TableMixin],
  name: 'Product',
  data () {
    return {
      columns: [
        {
          title: 'BOM编号',
          dataIndex: 'number'
        },
        {
          title: '产品名称',
          dataIndex: 'name'
        },
        {
          title: '版本号',
          dataIndex: 'version'
        },
        {
          title: '是否生效',
          dataIndex: 'iseffective',
          customRender: (record) => {
            return (
              <div>
                {
                  this.statusList.map(item => {
                    if (item.value === record) {
                      return (
                        <label>{item.label}</label>
                      )
                    }
                  })
                }
              </div>
            )
          }
        },
        {
          title: '发布日期',
          dataIndex: 'releaseDate'
        },
        {
          title: '特殊属性(客户)',
          dataIndex: 'attribute'
        },
        {
          title: '操作',
          width: '350px',
          customRender: (text, record, index) => {
            return (
              <div>
                <a onClick={(e) => this.show(record)}
                >查看</a>
              </div>
            )
          }
        }
      ],
      queryParam: {
        name: ''
      },
      statusList: [
        {
          label: '是',
          value: '1'
        },
        {
          label: '否',
          value: '2'
        }
      ],
      loadData: parameter => {
        return fetchList(
          Object.assign(parameter, this.queryParam)
        ).then(res => {
          return res.data
        })
      }
    }
  },
  methods: {
    goToUpload () {
      this.$refs.ProductUpload.show()
    },
    handleChange (info) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList)
      }
      if (info.file.status === 'done') {
        if (info.file.response.code === 0) {
          this.$message.success('导入成功！')
          this.init()
        } else if (info.file.response.code === 1) {
          this.$message.error(info.file.response.msg)
        }
        this.queryPage()
      } else if (info.file.status === 'error') {
        this.$message.error('导入失败，请重试！')
      }
    },
    show (record) {
      this.$router.push({ name: 'ShowBoms', query: { number: record.number } })
    }
  }
}
</script>

<style scoped>

</style>
